<template>
  <Panel>
    <template #tit>商品分类</template>
    <template>
      <!-- 1.按钮 -->
      <el-button type="primary" size="mini" @click="dialogVisible = true"
        >添加</el-button
      >
      <!-- 1.1弹框 -->
      <el-dialog title="添加分类" :visible.sync="dialogVisible" width="50%">
        <div>
          <!-- 这里写表单 -->
          <el-form
            :model="cateFrom"
            :rules="rules"
            ref="ruleForm"
            label-width="100px"
            class="demo-ruleForm"
            size="mini"
          >
            <el-form-item label="活动名称" prop="cateName">
              <el-input v-model="cateFrom.cateName"></el-input>
            </el-form-item>
            <el-form-item prop="state" label="是否启用">
              <template>
                <el-switch
                  v-model="cateFrom.state"
                  active-color="#13ce66"
                  inactive-color="#ff4949"
                >
                </el-switch>
              </template>
            </el-form-item>
          </el-form>
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addGoods('ruleForm')"
            >确 定</el-button
          >
        </span>
      </el-dialog>
      <!-- 2.表格 -->
      <el-table :data="tableData" style="width: 100%">
        <el-table-column type="index" label="序号" width="180">
        </el-table-column>
        <el-table-column label="分类名称">
          <!-- 利用插槽获的方式解构row,获取到当前表单的信息 -->
          <template v-slot="{ row }">
            <el-input
              v-model="row.cateName"
              v-if="row.flag"
              size="mini"
            ></el-input>
            <span v-else>{{ row.cateName }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="state" label="是否启用">
          <template v-slot="{ row }">
            <el-switch
              v-model="row.state"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              :inactive-value="0"
              :disabled="!row.flag"
            >
            </el-switch>
            <!-- {{ row.flag }} -->
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <!-- 使用插槽显示按钮 -->
          <template v-slot="{ row }">
            <el-button
              :type="row.flag ? 'success' : 'primary'"
              size="mini"
              @click="fn(row)"
              >{{ row.flag ? '完成' : '编辑' }}</el-button
            >
            <el-button type="danger" size="mini" @click="delcate(row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 3.分页 -->
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </template>
  </Panel>
</template>

<script>
//2.1导出获取商品分类的方法
import { getCates, addgetCates, delCates, editcate } from '../../api/goods'

export default {
  data() {
    return {
      tableData: [],
      //当前第几页
      currentPage: 1,
      //总页数
      total: 0,
      //每页显示多少条
      pageSize: 5,
      dialogVisible: false,
      cateFrom: {
        cateName: '',
        state: true
      },
      rules: {
        cateName: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 8 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getCates()
    console.log(this.row, 'row')
  },
  methods: {
    //一上来就需要发请求,获取数据,并且渲染
    async getCates() {
      const { data, total } = await getCates({
        currentPage: this.currentPage,
        pageSize: this.pageSize
      })
      // console.log(data, '2222222222')
      // console.log(total, '1111111111111')
      this.total = total
      this.tableData = data
      this.tableData.forEach(item => this.$set(item, 'flag', false))
    },
    handleSizeChange(val) {
      this.pageSize = val
      console.log(`每页 ${val} 条`)
      this.getCates()
    },
    handleCurrentChange(val) {
      console.log(`当前第几页: ${val}`)
      this.currentPage = val
      this.getCates()
    },
    //编辑
    async fn(row) {
      // console.log(row, 'ROW')
      // 这里不能直接传row,因为接口要的state是布尔值,所有需要转换
      if (row.flag) {
        if (row.cateName === '') return
        const { code, msg } = await editcate({
          id: row.id,
          cateName: row.cateName,
          state: Boolean(row.flag)
        })
        if (code === 0) {
          this.$message.success(msg)
          this.getCates()
          row.flag = false
        }
      } else {
        row.flag = true
      }
    },
    //添加分类
    addGoods(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          const { code, msg } = await addgetCates(this.cateFrom)
          if (code === 0) {
            this.$message.success(msg)
            this.getCates()
            this.dialogVisible = false
            this.$refs[formName].resetFields()
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    //删除单个
    delcate(id) {
      this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const { code, msg } = await delCates({ id })
          if (code === 0) {
            this.$message.success(msg)
            this.getCates()
          }
          // console.log(data)
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  }
}
</script>
<style lang="less" scoped>
.el-table {
  margin: 20px 0 40px;
}
</style>
